/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import boundless.ejb.RequestBean;
import boundless.entities.Account;
import boundless.entities.Invitebyid;
import boundless.entities.Joinrequests;
import boundless.entities.League;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;

/**
 *
 * @author hendrikdekock
 */
@Named(value = "accountBean")
@Dependent
@SessionScoped
public class AccountBean {

    @EJB
    RequestBean rb;
    
    @Inject
    Account curAccount;
    
    List<Invitebyid> pendingInvitations;
    
    String error;
    
    /**
     * Creates a new instance of AccountBean
     */
    public AccountBean() {
    }
    
    
    //For Testing
    public void setAccount(Account a) {
        curAccount = a;
    }
    
    public void setRequestBean(RequestBean r) {
        rb = r;
    }
    
    public void viewInvitations() {
        pendingInvitations = rb.getInvitationsForUser(curAccount);
        System.out.println("NUMBER OF INVITATIONS " + pendingInvitations.size());
    }
    
    public List<Invitebyid> getInvitations() {
        return pendingInvitations;
    }
    
    /**
     * Accepts an invitation to a particular team--removes the invitation from the list and 
     * Adds the user to the team.  They still need to make a team
     * @param invite : The invitation to accept
     */
    public void acceptInvitation(Invitebyid invite) {
        Account a = rb.getAccountById(invite.getInvitebyidPK().getInviteid());
        League l = rb.getLeague(invite.getInvitebyidPK().getLeagueid());
        a.getLeagueCollection().add(l);
        l.getAccountCollection().add(a);
        rb.removeInvitation(invite);
        rb.removeJoinRequest(invite);
        
    }
    
    /**
     * Decline a by-id invite (by-email invites are turned into by-id invites)
     * @param invite : the invitation to decline
     */
    public void declineInvitation(Invitebyid invite) {
        rb.removeInvitation(invite);
        rb.removeJoinRequest(invite);
    }
    
    /**
     * Makes a join request to join the specified league.  
     * @param l : The league you wish to join
     * @return : True if a request was made, false if the League is invite only
     */
    public boolean requestToJoinLeague(League l) {
        if (l.getInviteonly() == 1)
            return false;
        Joinrequests jr = new Joinrequests(curAccount, l);
        rb.addJoinRequest(jr);
        return true;
        
    }
    
    public List<League> getAllLeaguesForThisUser() {
        List<League> allLeagues = rb.getAllLeagues();
        ArrayList<League> myLeagues = new ArrayList<League>();
        for (League l : allLeagues) {
            if (l.isUserMember(curAccount)) {
                myLeagues.add(l);
            }
        }
        return myLeagues;
    }
}
